Part.1 Train deep learning models

0. Import the libraries

1. Data Pre-processing

data = data.DataReader("^GSPC", start='2012-03-31', end='2020-03-31', data_source='yahoo') data

setup the training and testing datasets

data normalization

setup the labels and input sequence

general parameters

2.1 LSTM Model

predict the testset andd visulize the result

2.2 LSTM deeper Model

predict the testset andd visulize the result

3.1 Simple Neural Network Model

predict the testset and visulize the result

3.2 Simple Neural Network Model_deeper

predict the testset and visulize the result

4.1 Convolutional Neural Network Model

test set preprocessing

4.2 Convolutional Neural Network Model_deeper

5.1 ResNet Model-1D

5.2 1D-ResNet 1 layer

5.3 1D-ResNet-deeper

6.1 ARIMA

https://levelup.gitconnected.com/build-an-arima-model-to-predict-a-stocks-price-c9e1e49367d3

import numpy as np import pandas as pd import matplotlib.pyplot as plt from pandas.plotting import lag_plot from pandas import datetime from statsmodels.tsa.arima_model import ARIMA from sklearn.metrics import mean_squared_error

df= data df.head()

plt.figure(figsize=(10,10)) lag_plot(df['Close'], lag=10) plt.title('Autocorrelation plot')

train_data, test_data = data_training, data_test plt.figure(figsize=(12,7)) plt.title('SPY Prices') plt.xlabel('Dates') plt.ylabel('Prices') plt.plot(df['Adj Close'], 'blue', label='Training Data') plt.plot(test_data['Adj Close'], 'green', label='Testing Data') plt.legend()

def smape_kun(y_true, y_pred): return np.mean((np.abs(y_pred - y_true) * 200/ (np.abs(y_pred) + np.abs(y_true))))

train_ar = train_data['Adj Close'].values test_ar = test_data['Adj Close'].values history = [x for x in train_ar] print(type(history)) predictions = list()

for t in range(len(test_ar)):

model = ARIMA(history, order=(5,1,0))
model_fit = model.fit(disp=0)
output = model_fit.forecast()

#print(model)
#print(history)
#print(output)


yhat = output[0]
predictions.append(yhat)
obs = test_ar[t]
history.append(obs)
#print('predicted=%f, expected=%f' % (yhat, obs))

error = mean_squared_error(test_ar, predictions) print('Testing Mean Squared Error: %.3f' % error) error2 = smape_kun(test_ar, predictions) print('Symmetric mean absolute percentage error: %.3f' % error2)

Visualising the results

y_test_iv = test_ar y_pred_iv =predictions

Visualising the results

plt.figure(figsize=(14,5)) plt.plot(y_test_iv, color = 'red', label = 'Actual SPY Price') plt.plot(y_pred_iv, color = 'blue', label = 'Predicted SPY Price') plt.title('SPY Price Prediction') plt.xlabel('Time') plt.ylabel('SPY Price') plt.legend() plt.show()

print('RMSE') rmse= mean_squared_error(y_test_iv, y_pred_iv, squared=False) print(rmse)

print('MAE') mae = mean_absolute_error(y_test_iv, y_pred_iv) print(mae)

print('R2') r2 = r2_score(y_test_iv, y_pred_iv) print(r2)

print('total absolute error') tae = sum(abs(y_test_iv-y_pred_iv))[0] print(tae)

==================================================================================================

y_pred_iv_pct = [((y_pred_iv[i]/y_pred_iv[i-1]) -1) 100 for i in range(1,len(y_pred))] y_test_iv_pct = [((y_test_iv[i]/y_test_iv[i-1]) -1) 100 for i in range(1,len(y_pred))]

print('rate of change accuracy') rc = sum([y_pred_iv_pct[i]*y_test_iv_pct[i]>0 for i in range(len(y_test_iv_pct))])/len(y_test_iv_pct) print(rc[0])

df_result['LSTM'] = [rmse,mae,r2,tae,rc]

Visualising the results

plt.figure(figsize=(14,5)) plt.plot(y_test_iv_pct, color = 'red', label = 'Actual SPY change rate') plt.plot(y_pred_iv_pct, color = 'blue', label = 'Predicted SPY change rate') plt.title('SPY Price Prediction') plt.xlabel('Time') plt.ylabel('SPY Price change rate') plt.legend() plt.show()

Visualising the results

plt.figure(figsize=(14,5)) plt.plot(y_test_iv_pct[:30], color = 'red', label = 'Actual SPY change rate') plt.plot(y_pred_iv_pct[:30], color = 'blue', label = 'Predicted SPY change rate') plt.title('SPY Price Prediction') plt.xlabel('Time') plt.ylabel('SPY Price change rate') plt.legend()

ax = plt.axes()
plt.grid(axis='both', which='both') plt.show()

df_result['ARIMA'] = [rmse,mae,r2,tae]

SUMMARY

# Part.2======Multi-armed bandit All models==============

import models

import data

The best model

Epsilon-greedy bandit

part.2 epsilon loop test

UCB1 bandit

part2 ucb1 loop test

Exp3 bandit

part.2 exp3 loop test

# Part.3=========Multi-armed bandit 5 Selected models=============

import models

import data

Epsilon-greedy bandit

part.3 epsilon loop test

UCB1 bandit

Exp3 bandit

part3.exp3 loop test

# Part.4=========Multi-armed bandit 3 Selected models=============

import models

import data

Epsilon-greedy bandit

part.4 epsilon loop test

UCB1 bandit

part4. ucb1 loop test

Exp3 bandit

part.4 exp3 loop test

bandit summary single test

bandit summary loop test